package com.yanqu.road.logic.cross;

import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.entity.tradewar.TradeWarWanted;
import com.yanqu.road.logic.helper.RankListHelper;
import com.yanqu.road.logic.pb.CrossRankPb;
import com.yanqu.road.logic.sort.CrossRankSort;
import com.yanqu.road.pb.rank.RankProto;

import java.math.BigInteger;
import java.util.LinkedList;

public class CrossTradeWarWantedRankListModel {

    private CrossRankSort crossRankSort;
    private LinkedList<CrossUserRank> rankList;
    private int activityId;
    private RankProto.RankListMsg.Builder rankMsg;

    public CrossTradeWarWantedRankListModel(int activityId) {
        crossRankSort = new CrossRankSort();
        rankList = new LinkedList<>();
        this.activityId = activityId;
        rankMsg = RankProto.RankListMsg.newBuilder();
    }

    public int getActivityId() {
        return activityId;
    }

    public RankProto.RankListMsg.Builder getRankListMsg() {
        return rankMsg;
    }

    public LinkedList<CrossUserRank> getRankList() {
        return new LinkedList<>(rankList);
    }

    public void setRankList(LinkedList<CrossUserRank> userRankList) {
        synchronized (this) {
            rankList = new LinkedList<>(userRankList);
            RankListHelper.checkUserRankListValid(rankList);       //校验一下数组
            rankList.sort(crossRankSort);
            rankMsg = initRankMsg();
        }
    }

    private RankProto.RankListMsg.Builder initRankMsg() {
        LinkedList<CrossUserRank> tempList;
        synchronized (this){
            tempList = new LinkedList<>(rankList);
        }
        return CrossRankPb.parseRankListMsg(tempList);
    }

    public int getMyRank(long userId, long serverId) {
        int rank = -1;
        synchronized (this) {
            for(int i = 0; i < rankList.size(); i++){
                CrossUserRank userRank = rankList.get(i);
                if(userRank.getUserId() == userId && userRank.getUserBaseInfo().getServerId() == serverId){
                    rank = i + 1;
                    break;
                }
            }
        }
        return rank;
    }

    public CrossUserRank getMyRankInfo(long userId, long serverId) {
        synchronized (this) {
            for(CrossUserRank userRank : rankList){
                if(userRank.getUserId() == userId && userRank.getUserBaseInfo().getServerId() == serverId){
                    return userRank;
                }
            }
        }
        return null;
    }

    public RankProto.RankTempMsg.Builder getTopRankMsg() {
        synchronized (this) {
            for(CrossUserRank userRank : rankList){
                return CrossRankPb.parseRankTempMsg(userRank);
            }
        }
        return null;
    }

    /**
     * 添加到排行
     */
    public void rankChange(TradeWarWanted tradeWarWanted, BigInteger addValue) {
        synchronized (this) {
            CrossUserRank userRank = getMyRankInfo(tradeWarWanted.getUserId(), tradeWarWanted.getUserBaseInfo().getServerId());
            if(null == userRank){
                userRank = new CrossUserRank();
                userRank.setUserId(tradeWarWanted.getUserId());
                userRank.setValue(BigInteger.ZERO);
                rankList.add(userRank);
            }
            userRank.setUserBaseInfo(tradeWarWanted.getUserBaseInfo());
            userRank.setLastUpdateTime(tradeWarWanted.getWantedTime());
            userRank.setValue(userRank.getValue().add(addValue));
            rankList.sort(crossRankSort);
            rankMsg = initRankMsg();
        }
    }
}
